<?php
class it_role_model extends MY_Model {
	var $model = 'it_role';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	A.*, B.APP_NAME
					FROM	IT_ROLE A
							JOIN IT_APPLICATION B ON A.APP_ID = B.APP_ID
					WHERE	(UPPER(role_id) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(role_name) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(description) LIKE '%".strtoupper(post('keyword'))."%')
							".(APP == 'CORE' ? (post('app_id')=='' ? "" : "  A.APP_ID = '".post('app_id')."' ") : " AND A.APP_ID = '".APP."' ")."
							AND role_id <> 'DEV'
					ORDER BY ".(post('sort')=='' ? 'role_id' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('it_role', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('it_role', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('it_role', array('role_id' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('it_role', array('role_id' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM it_user_role WHERE role_id = '".$arr_rule['role_id']."'"))>0)
			{
				return $this->error_delete.' [id: '.$arr_rule['role_id']."]<br />";
			}else{
				$this->db->delete('it_role', array('role_id' => $arr_rule['role_id']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>